package com.trip.config;

import com.trip.exception.ServiceException;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author hc24
 * @version 2017/9/27
 */
@ConfigurationProperties(prefix = "com.trip")
@Getter
@Setter
public class CommonProperties {

    private UploadPicConfig picConfig;

    private Atlas atlas;

    public void validatePic(MultipartFile file){
        //content type
        if(!this.getPicConfig().getContentTypes()
                .contains(file.getContentType())){
            throw new ServiceException(file.getContentType()+" is not allowed.");
        }

        //max size
        if(this.getPicConfig().getMaxSize()<file.getSize()){
            throw new ServiceException("File exceed the max size "+this.getPicConfig().getMaxSizeM()+"M");
        }
    }

    @Getter
    @Setter
    public static class Atlas{
        private String url;
        private String key;
        private String cats;
    }

    public static class UploadPicConfig{
        private long maxSize;
        private List<String> contentTypes;
        private String picFolder;
        public long getMaxSize() {
            return maxSize;
        }
        public void setMaxSize(long maxSize) {
            this.maxSize = maxSize;
        }
        public List<String> getContentTypes() {
            return contentTypes;
        }
        public void setContentTypes(List<String> contentTypes) {
            this.contentTypes = contentTypes;
        }

        public long getMaxSizeM(){
            return this.maxSize/1024/1024;
        }

        public String getPicFolder() {
            return picFolder;
        }

        public void setPicFolder(String picFolder) {
            this.picFolder = picFolder;
        }
    }

    public UploadPicConfig getPicConfig() {
        return picConfig;
    }

    public void setPicConfig(UploadPicConfig picConfig) {
        this.picConfig = picConfig;
    }
}
